package com.lhz.project.system.task;

import com.lhz.CustomizeApplicationContext;
import com.lhz.project.system.model.entity.task.TaskLog;
import com.lhz.project.system.service.task.TaskService;
import lombok.extern.slf4j.Slf4j;

/**
 * @Author: LiHuaZhi
 * @Date: 2021/9/26 23:11
 * @Description:
 **/
@Slf4j
public class TaskLogRecord {

    public static void recordTaskLog(String taskId, long currentTime, Exception e) {
        long recordTime = System.currentTimeMillis();
        int status = 0;//正常
        String exceptionInfo = "";

        if (e != null) {
            status = 1;//异常
            exceptionInfo = e.getMessage().length() > 500 ? e.getMessage().substring(0, 500) : e.getMessage();
        }

        //记录日志
        TaskLog taskLog = new TaskLog();
        taskLog.setTaskId(taskId);
        taskLog.setStatus(status);
        if (currentTime > 0) {
            taskLog.setTime(Long.toString(recordTime - currentTime));
        }
        taskLog.setExceptionInfo(exceptionInfo);

        //插入记录
        CustomizeApplicationContext.getBean(TaskService.class).insertTaskLog(taskLog);
    }
}
